/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import ru.rantipin.media.MediaHelper;
import ru.rantipin.utilities.CaptureDiviceStarterListener;

/**
 *
 * @author rantipin
 */
public class CaptureDialog extends JDialog{
    
    private static final String ICON = "/images/capture.png";
    private static final int LIST_COUNT = 4;
    
    public CaptureDialog(JFrame main, CaptureDiviceStarterListener listner) {
        
        this.listener = listner;
        
        setTitle("Select capture device");
        
        ImageIcon icon = new ImageIcon(getClass().getResource(ICON));
        setIconImage(icon.getImage());
        
        setModal(true);
        setSize(300, 200);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        initComponentsLayout();
        
        setLocationRelativeTo(main);
        setVisible(true);
    }
    
    private void initComponentsLayout() {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        
        JLabel labelDevice = new JLabel("Devices of RGB encoding:");
        gridbag.setConstraints(labelDevice, 
                new GridBagConstraints(
                    0, 0,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(labelDevice);
        
        gridbag.setConstraints(getDeviceList(), 
                new GridBagConstraints(
                    0, 1,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getDeviceList());
        
        JLabel labelSize = new JLabel("Video size:");
        gridbag.setConstraints(labelSize, 
                new GridBagConstraints(
                    0, 2,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(labelSize);
        
        gridbag.setConstraints(getVideoSize(), 
                new GridBagConstraints(
                    1, 2,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getVideoSize());
        
        JLabel labelBitPerPixel = new JLabel("Bit per Pixel:");
        gridbag.setConstraints(labelBitPerPixel, 
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(labelBitPerPixel);
        
        gridbag.setConstraints(getBitPerPixel(), 
                new GridBagConstraints(
                    1, 3,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getBitPerPixel());
        
        gridbag.setConstraints(getOkButton(), 
                new GridBagConstraints(
                    1, 4,
                    1, 1,
                    1.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 10),
                    0, 0
                ));
        add(getOkButton());
        
        gridbag.setConstraints(getCancelButton(), 
                new GridBagConstraints(
                    2, 4,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 0, 10, 10),
                    0, 0
                ));
        add(getCancelButton());
    }
    
    private JComboBox getDeviceList() {
        if (deviceList == null) {
            deviceList = new JComboBox();
            deviceList.setMaximumRowCount(LIST_COUNT);
            
            addDeviceItems();
            
            deviceList.addActionListener(new ActionListener() {
                
                public void actionPerformed(ActionEvent e) {
                    
                }
            });
        }
        
        return deviceList;
    }
    
    private JComboBox getVideoSize() {
        if (videoSize == null) {
            videoSize = new JComboBox();
            videoSize.setMaximumRowCount(LIST_COUNT);
                    
            addVideoSizeItems();
            
            videoSize.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    addPitPerPixelItems();
                }
            });
        }
        
        return videoSize;
    }
    
    private JComboBox getBitPerPixel() {
        if (bitPerPixel == null) {
            bitPerPixel = new JComboBox();
            bitPerPixel.setMaximumRowCount(LIST_COUNT);
            
            addPitPerPixelItems();
        }
        
        return bitPerPixel;
    }
    
    private void addDeviceItems() {
        Set<String> names = MediaHelper.getInstance().getDeviceInfo();
        deviceList.removeAllItems();
        for(String s: names) {
            deviceList.addItem(s);
        }
    }
    
    private void addVideoSizeItems() {
        String deviceName = (String)getDeviceList().getSelectedItem();
        Set<String> size = MediaHelper.getInstance().getSize(deviceName);
        videoSize.removeAllItems();
        for(String s: size) {
            videoSize.addItem(s);
        }
    }
    
    private void addPitPerPixelItems() {
        String devName = (String)getDeviceList().getSelectedItem();
        String sizeName = (String)getVideoSize().getSelectedItem();
        bitPerPixel.removeAllItems();
        if (!"".equals(devName) && !"".equals(sizeName)) {
            Set<String> bits = MediaHelper.getInstance().getBitsPerPixel(devName, sizeName);
            for(String s: bits) {
                bitPerPixel.addItem(s);
            }
        }
    }
    
    private JButton getOkButton() {
        if (buttonOK == null) {
            buttonOK = new JButton("OK");
            buttonOK.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    CaptureDialog.this.dispose();
                    listener.startMediaSource(
                            (String) getDeviceList().getSelectedItem(), 
                            (String) getVideoSize().getSelectedItem(), 
                            (String) getBitPerPixel().getSelectedItem());
                }
            });
        }
        
        return buttonOK;
    }

    private JButton getCancelButton() {
        if (buttonCancel == null) {
            buttonCancel = new JButton("Cancel");
            buttonCancel.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    CaptureDialog.this.dispose();
                }
            });
        }
        
        return buttonCancel;
    }
    
    private CaptureDiviceStarterListener listener = null;
    private JComboBox deviceList = null;
    private JComboBox videoSize = null;
    private JComboBox bitPerPixel = null;
    private JButton buttonOK = null;
    private JButton buttonCancel = null;
}
